package com.inforich.app.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.inforich.app.api.dto.ProjectRubbishRemovalDTO;
import com.inforich.app.api.service.IAppProjectRubbishRemovalService;
import com.inforich.app.api.service.IAppProjectService;
import com.inforich.app.component.LoginUserApp;
import com.inforich.common.constant.ProjectConstants;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.ApproveEnum;
import com.inforich.common.enums.StageEnum;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.domain.ProjectRubbishExpand;
import com.inforich.projectManage.domain.ProjectRubbishRemoval;
import com.inforich.projectManage.service.IProjectRubbishDetailService;
import com.inforich.projectManage.service.IProjectRubbishExpandService;
import com.inforich.projectManage.service.IProjectRubbishRemovalService;
import com.inforich.projectManage.service.IProjectService;
import java.util.Objects;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/30
 */
@Service
public class AppProjectRubbishRemovalServiceImpl implements IAppProjectRubbishRemovalService {

  @Autowired
  private IProjectRubbishRemovalService projectRubbishRemovalService;
  @Autowired
  private IAppProjectService appProjectService;
  @Autowired
  private IProjectRubbishDetailService projectRubbishDetailService;
  @Autowired
  private IProjectService projectService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AjaxResult addProjectRemoval(LoginUserApp loginUserApp, ProjectRubbishRemovalDTO dto) {
    int count = projectRubbishRemovalService.countByProjectId(dto.getProjectId());
    if (count > 0) {
      return AjaxResult.error("不可重复添加拆迁备案");
    }
    Project project = projectService.getProjectById(dto.getProjectId());
    if (ProjectConstants.PROJECT_KIND_EXPAND.equals(project.getProjectKindId())) {
      return AjaxResult.error("当前项目为新扩改项目，不可进行拆迁备案");
    }
    if (ProjectConstants.PROJECT_KIND_RENOVATION.equals(project.getProjectKindId())) {
      return AjaxResult.error("当前项目为新扩改项目，不可进行拆迁备案");
    }
    // 可利用重量不能大于预计垃圾重量
    if (dto.getRubbishRecycleWeight().compareTo(dto.getRubbishPlanWeight()) == 1) {
      return AjaxResult.error("可利用重量不能大于预计垃圾重量");
    }
    ProjectRubbishRemoval pre = new ProjectRubbishRemoval();
    BeanUtils.copyProperties(dto, pre);
    if ("1".equals(dto.getIsSubmit())) {
      pre.setApprovalStatus(ApproveEnum.REVIEW.getDbData());
    }

    pre.setClassifyStage(CollUtil.isNotEmpty(dto.getRubbishClassifyList()) ? "1" : "0");
    pre.setRecycleStage(CollUtil.isNotEmpty(dto.getRubbishRecycleList()) ? "1" : "0");
    // 保存垃圾明细
    String projectId = dto.getProjectId();
    projectRubbishDetailService.saveDetail(dto.getRubbishClassifyList(), projectId,
        StageEnum.CLASSIFY_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishRecycleList(), projectId,
        StageEnum.RECYCLE_STAGE);
    return AjaxResult.success(projectRubbishRemovalService.save(pre));
  }

  @Override
  public AjaxResult updateProjectRemoval(LoginUserApp loginUserApp, ProjectRubbishRemovalDTO dto) {
    String id = dto.getId();
    ProjectRubbishRemoval pre = projectRubbishRemovalService.getProjectRubbishRemovalById(id);
    if (Objects.equals(ApproveEnum.REVIEW.getDbData(), pre.getApprovalStatus())) {
      return AjaxResult.error("待审核的新改扩登记不可修改");
    }
    if (Objects.equals(ApproveEnum.PASS.getDbData(), pre.getApprovalStatus())) {
      return AjaxResult.error("已通过的新改扩登记不可修改");
    }
    BeanUtils.copyProperties(dto, pre);
    if ("1".equals(dto.getIsSubmit())) {
      pre.setApprovalStatus(ApproveEnum.REVIEW.getDbData());
    }

    pre.setClassifyStage(CollUtil.isNotEmpty(dto.getRubbishClassifyList()) ? "1" : "0");
    pre.setRecycleStage(CollUtil.isNotEmpty(dto.getRubbishRecycleList()) ? "1" : "0");
    // 保存垃圾明细
    String projectId = dto.getProjectId();
    projectRubbishDetailService.saveDetail(dto.getRubbishClassifyList(), projectId,
        StageEnum.CLASSIFY_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishRecycleList(), projectId,
        StageEnum.RECYCLE_STAGE);
    return AjaxResult.success(projectRubbishRemovalService.updateById(pre));
  }

  @Override
  public AjaxResult myProjectRemoval(LoginUserApp loginUserApp) {
    Project project = appProjectService.getMyProject(loginUserApp);
    if (project == null) {
      return AjaxResult.error("请先进行项目认证");
    }
    ProjectRubbishRemoval re = projectRubbishRemovalService.getProjectRubbishRemovalByProjectId(
        project.getId());
    if (re == null) {
      re = new ProjectRubbishRemoval();
      re.setProjectId(project.getId());
      re.setProjectName(project.getName());
      re.setProjectAddress(project.getAddress());
      re.setProjectKindId(project.getProjectKindId());
      re.setProjectLocation(project.getLocation());
    }
    return AjaxResult.success(re);
  }
}
